//https://leetcode.cn/problems/fibonacci-number/submissions/
package codeRandomThoughts.Test509斐波那契数;

/**
 * 递归写法
 */
public class Solution3 {
    public int fib(int n) {
        if (n <= 1) return n;
        //dp[i]:斐波那契数F(i)
        int[] dp = new int[2];
        dp[0] = 0;
        dp[1] = 1;

        for (int i = 2; i <= n; i++) {
            int sum = dp[0] + dp[1];
            dp[0] = dp[1];
            dp[1] = sum;
        }

        return dp[1];
    }
}
